package com.example.shoppingmall.home.adapter;



import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.example.shoppingmall.R;
import com.example.shoppingmall.shoppingcart.app.GoodsInfoActivity;
import com.example.shoppingmall.base.GoodsBean;
import com.example.shoppingmall.home.bean.ResultBeanData;
import com.example.shoppingmall.utils.Constants;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerClickListener;
import com.youth.banner.listener.OnLoadImageListener;
import com.zhy.magicviewpager.transformer.ScaleInTransformer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

// TODO 难点
public class HomeFragmentAdatpter extends RecyclerView.Adapter {
    /**
     *  广告条幅类型
     * */
    public static final int BANNER = 0;
    /**
     *  频道
     * */
    public static final int CHANNEL = 1;
    /**
     *  活动
     * */
    public static final int ACT = 2;
    /**
     * 秒杀
     * */
    public static final int SECKILL = 3;
    /**
     * 推荐
     * */
    public static final int RECOMMEND = 4;
    /**
     * 热卖
     * */
    public static final int HOT = 5;
    private static final String GOODS_BEAN = "goodsBean";
    private  Context mContext;
    private  ResultBeanData.ResultBean resultBean;
    private LayoutInflater mLayoutInflater;


    // 当前类型
    private int currentType = 0;

    public HomeFragmentAdatpter(Context mContext, ResultBeanData.ResultBean resultBean) {
        this.mContext = mContext;
        this.resultBean = resultBean;
        // 实列化数据
        mLayoutInflater = LayoutInflater.from(mContext);

    }


    /**
     *  相当于getView
     *
     * */
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 轮播图
        if(viewType == BANNER){
            return new BannerViewHolder(mContext,mLayoutInflater.inflate(R.layout.banner_viewpager,null));

        }else if(viewType == CHANNEL){  // 菜单导航栏
            return new ChannelViewHoler(mContext,mLayoutInflater.inflate(R.layout.channel_item,null));
        }else if(viewType == ACT){  // 活动
            return new ActViewHolder(mContext,mLayoutInflater.inflate(R.layout.act_item,null));
        }else if(viewType == SECKILL){  // 秒杀  --1
            return new SeckillViewHolder(mContext,mLayoutInflater.inflate(R.layout.seckill_item,null));

        }else if(viewType == RECOMMEND){  // 新品推荐
            return new RecommendViewHolder(mContext,mLayoutInflater.inflate(R.layout.recommend_item,null));
        }else if(viewType == HOT){  // hot买
            return new HotViewHolder(mContext,mLayoutInflater.inflate(R.layout.hot_item,null));


        }
        return null;
    }
    
    // 热卖
    class HotViewHolder extends RecyclerView.ViewHolder{

        private final Context mContext;
        private GridView gv_hot;
        private TextView tv_more_hot;

        public HotViewHolder(Context mContext, @NonNull View itemView) {
            super(itemView);
            this.mContext = mContext;
            tv_more_hot = (TextView) itemView.findViewById(R.id.tv_more_hot);
            gv_hot = (GridView) itemView.findViewById(R.id.gv_hot);
        }

        public void setData(List<ResultBeanData.ResultBean.HotInfoDTO> hotInfo) {
            // 1.有数据
            // 2.设置GridView的适配器
            HotGridViewAdapter adapter = new HotGridViewAdapter(mContext,hotInfo);
            gv_hot.setAdapter(adapter);

            // 设置item监听
            gv_hot.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Toast.makeText(mContext, "i==" + i, Toast.LENGTH_SHORT).show();
                    // 热卖商品类
                    ResultBeanData.ResultBean.HotInfoDTO hotInfoDTO = hotInfo.get(i);
                    // 商品信息类
                    // 传递数据页面
                    GoodsBean goodsBean = new GoodsBean();
                    goodsBean.setCover_price(hotInfoDTO.getCoverPrice());
                    goodsBean.setFigure(hotInfoDTO.getFigure());
                    goodsBean.setName(hotInfoDTO.getName());
                    goodsBean.setProduct_id(hotInfoDTO.getProductId());
                    startGoodsInfoActivity(goodsBean);


                }
            });


        }
    }

    //新品推荐
     class RecommendViewHolder extends RecyclerView.ViewHolder{
        private  Context mContext;
        private  RecommendGridViewAdapter adapter;
        //private static Context mContext;
        //private final Context mContext;
        private TextView tv_more_recommend;
        private GridView gv_recommend;
        //private RecommendGridViewAdapter adapter;

        public RecommendViewHolder(Context mContext, @NonNull View itemView) {
            super(itemView);
            this.mContext = mContext;
            tv_more_recommend = (TextView) itemView.findViewById(R.id.tv_more_recommend);
            gv_recommend = (GridView) itemView.findViewById(R.id.gv_recommend);
        }

        public void setData(final List<ResultBeanData.ResultBean.RecommendInfoDTO> recommendInfo) {
            //1 有数据了
            // 2 设置设配器
            adapter = new RecommendGridViewAdapter(mContext,recommendInfo);
            gv_recommend.setAdapter(adapter);

            gv_recommend.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Toast.makeText(mContext, "i ==" + i, Toast.LENGTH_SHORT).show();
                    ResultBeanData.ResultBean.RecommendInfoDTO recommendInfoDTO = recommendInfo.get(i);

                    GoodsBean goodsBean = new GoodsBean();
                    goodsBean.setProduct_id(recommendInfoDTO.getProductId());
                    goodsBean.setCover_price(recommendInfoDTO.getCoverPrice());
                    goodsBean.setFigure(recommendInfoDTO.getFigure());
                    goodsBean.setName(recommendInfoDTO.getName());
                    startGoodsInfoActivity(goodsBean);


                }
            });





        }
    }

    // 秒杀 --- 2
    class SeckillViewHolder extends RecyclerView.ViewHolder{
        // 初始化
        private final Context mContext;
        private TextView tv_time_seckill;
        private TextView tv_more_seckill;
        private RecyclerView rv_seckill;
        private SeckillRecyclerViewAdapter adapter;

        /**
         * 相差多少时间 毫秒
         * */
        private long dt = 0;
        private Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg){
                super.handleMessage(msg);
                dt = dt - 1000;
                SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
                String time = formatter.format(new Date(dt));
                tv_time_seckill.setText(time);

                handler.removeMessages(0);
                handler.sendEmptyMessageDelayed(0,1000);
                if(dt <= 0){
                    // 把消息移除
                    handler.removeCallbacksAndMessages(null);
                }

            }

        };

        public SeckillViewHolder(Context mContext, @NonNull View itemView) {
            super(itemView);
            tv_time_seckill = (TextView) itemView.findViewById(R.id.tv_time_seckill);
            tv_more_seckill = (TextView) itemView.findViewById(R.id.tv_more_seckill);
            rv_seckill = (RecyclerView) itemView.findViewById(R.id.rv_seckill);
            this.mContext = mContext;
        }
         //  设置数据 ---4
        public void setData(ResultBeanData.ResultBean.SeckillInfoDTO seckillInfo) {
            // 1. 得到数据
            // 2. 设置数据：文本和RecyclerView数据
            adapter = new SeckillRecyclerViewAdapter(mContext,seckillInfo.getList());
            rv_seckill.setAdapter(adapter);

            // 设置布局管理器
            rv_seckill.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
            // 设置item的点击事件
            adapter.setOnSeckillRecyclerView(new SeckillRecyclerViewAdapter.OnSeckillRecyclerView() {
                @Override
                public void onItemClick(int position) {
                    Toast.makeText(mContext, "秒杀" + position, Toast.LENGTH_SHORT).show();

                    ResultBeanData.ResultBean.SeckillInfoDTO.ListDTO listDTO = seckillInfo.getList().get(position);
                    //startGoodsInfoActivity();
                    GoodsBean goodsBean = new GoodsBean();
                    goodsBean.setCover_price(listDTO.getCoverPrice());
                    goodsBean.setFigure(listDTO.getFigure());
                    goodsBean.setName(listDTO.getName());
                    goodsBean.setProduct_id(listDTO.getProductId());
                    startGoodsInfoActivity(goodsBean);


                }
            });
            //秒杀倒计时 -毫秒
            dt =   Integer.valueOf(seckillInfo.getEndTime()) - Integer.valueOf(seckillInfo.getStartTime());

            handler.sendEmptyMessageDelayed(0,1000);



        }
    }
    // 活动
    class ActViewHolder extends RecyclerView.ViewHolder{
        private Context mContext;
        private ViewPager act_viewpager;

        public ActViewHolder(Context mContext ,View itemView) {
            super(itemView);
            this.mContext = mContext;
            act_viewpager = (ViewPager) itemView.findViewById(R.id.act_viewpager);
        }

        public void setData(List<ResultBeanData.ResultBean.ActInfoDTO> actInfo) {
            act_viewpager.setPageMargin(20);

            act_viewpager.setOffscreenPageLimit(3);
            // 添加动画效果 setPageTransformer 决定动画效果 ViewPager动画第三方库（MagicViewPager）
            //act_viewpager.setPageTransformer(true,new RotateDownPageTransformer());
            act_viewpager.setPageTransformer(true,new ScaleInTransformer());
            // 1. 有数据了
            // 2.设置适配器
            act_viewpager.setAdapter(new PagerAdapter() {
                @Override
                public int getCount() {
                    return actInfo.size();
                }
                /**
                 * view 页面
                 * object instantiateItem 方法返回的值
                 *
                 * */

                @Override
                public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                    return view == object;
                }

                /**
                 *  container viewPager
                 *  position 对应页面的位置
                 *
                 * */

                @NonNull
                @Override
                public Object instantiateItem(@NonNull ViewGroup container, int position) {
                    ImageView imageView = new ImageView(mContext);
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    Glide.with(mContext).load(Constants.BASE_URL_IMAGE + actInfo.get(position).getIconUrl()).into(imageView);
                    // 添加到容器中
                    container.addView(imageView);

                    // 设置点击事件
                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(mContext, "position==" + position, Toast.LENGTH_SHORT).show();
                        }
                    });
                    return imageView;
                }

                @Override
                public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                    container.removeView((View) object);

                }
            });

        }
    }
    // 菜单导航栏
    class ChannelViewHoler extends RecyclerView.ViewHolder{
        private Context mContext;
        private GridView gv_channel;
        private ChannelAdapter adapter;
        public ChannelViewHoler(final Context mContext, View itemView) {
            super(itemView);
            this.mContext = mContext;
            gv_channel = (GridView) itemView.findViewById(R.id.gv_channel);

            // 设置item点击事件
            gv_channel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView parent, View view, int i, long l) {
                    Toast.makeText(mContext, "position== " +i, Toast.LENGTH_SHORT).show();
                }
            });

        }

        public void setData(List<ResultBeanData.ResultBean.ChannelInfoDTO> channelInfo) {
            // 得到数据了
            // 设置GridView的适配器
            // mContext 上下文
            // channelInfo 数据
            adapter = new ChannelAdapter(mContext,channelInfo);
            gv_channel.setAdapter(adapter);
        }
    }
    //
    // 轮播图
    class BannerViewHolder extends RecyclerView.ViewHolder{
        private Context mContext;
        private View itemView;
        private Banner banner;

        public BannerViewHolder(Context mContext, View itemView) {
            super(itemView);
            this.mContext = mContext;
            this.banner = (Banner) itemView.findViewById(R.id.banner);
        }

        public void setData(List<ResultBeanData.ResultBean.BannerInfoDTO> bannerInfo) {
            // 设置banner的数据
            List<String> imagesUrl = new ArrayList<>();
            for(int i = 0;i< bannerInfo.size();i++){
                String imagUrl = bannerInfo.get(i).getImage();
                imagesUrl.add(imagUrl);
            }
            // 设置循环指示点
            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
            // banner 手风琴效果
            banner.setBannerAnimation(Transformer.Accordion);
            // 得到图片地址
            banner.setImages(imagesUrl, new OnLoadImageListener() {
                @Override
                public void OnLoadImage(ImageView view, Object url) {
                    // 联网请求图片 glide
                    Glide.with(mContext).load(Constants.BASE_URL_IMAGE+url).into(view);


                }
            });

            // item的点击事件
            banner.setOnBannerClickListener(new OnBannerClickListener() {
                @Override
                public void OnBannerClick(int position) {
                    Toast.makeText(mContext, "position==" + position, Toast.LENGTH_SHORT).show();
                    //startGoodsInfoActivity();
                }
            });

        }
    }
    /**
     *  相当于getView中绑定数据模式
     * */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(getItemViewType(position) == BANNER){
            BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
            bannerViewHolder.setData(resultBean.getBannerInfo());
        }else if(getItemViewType(position) == CHANNEL){
            ChannelViewHoler channelViewHoler = (ChannelViewHoler) holder;
            channelViewHoler.setData(resultBean.getChannelInfo());
        }else if(getItemViewType(position) == ACT){
            ActViewHolder actViewHolder = (ActViewHolder) holder;
            actViewHolder.setData(resultBean.getActInfo());
        }else if(getItemViewType(position) == SECKILL){ // 绑定数据 --4
            SeckillViewHolder seckillViewHolder = (SeckillViewHolder) holder;
            seckillViewHolder.setData(resultBean.getSeckillInfo());
        }else if(getItemViewType(position) == RECOMMEND){
            RecommendViewHolder recommendViewHolder = (RecommendViewHolder) holder;
            recommendViewHolder.setData(resultBean.getRecommendInfo());
        }else if(getItemViewType(position) == HOT){
            HotViewHolder hotViewHolder = (HotViewHolder) holder;
            hotViewHolder.setData(resultBean.getHotInfo());
        }

    }

    /**
     * 启动商品信息列表页面
     * */

    private void startGoodsInfoActivity(GoodsBean goodsBean){

        Intent intent = new Intent(mContext, GoodsInfoActivity.class);
        intent.putExtra(GOODS_BEAN,goodsBean);
        mContext.startActivity(intent);
    }
    /**
     * 得到类型
     * */
    @Override
    public int getItemViewType(int position) {
        switch (position){
            case BANNER:
                currentType = BANNER;
                break;
            case CHANNEL:
                currentType = CHANNEL;
                break;
            case ACT:
                currentType = ACT;
                break;
            case SECKILL:
                currentType = SECKILL;
                break;
            case RECOMMEND:
                currentType = RECOMMEND;
                break;
            case HOT:
                currentType = HOT;
                break;
        }
        return currentType;
    }

    /**
     *  总共多少item
     * */
    @Override
    public int getItemCount() {
        // 开发过程中从 1---2
        return 6;
    }
}
